<template>
  <el-card class="w-full pt-[5px] pb0">
    <el-form :model="searchForm" label-width="96px" ref="searchFormRef">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="分类编号" prop="code">
            <el-input v-model="searchForm.code" placeholder="分类编号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目分类" prop="name">
            <el-input v-model="searchForm.name" placeholder="项目分类" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目级别" prop="projectLevelId">
            <CommonSelect
              :isFetch="false"
              :dataList="projectLevelList"
              placeholder="项目级别"
              v-model="searchForm.projectLevelId"
              valueKey="code"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="flex justify-end">
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <el-card class="w-full pt-[5px] mt-[10px]">
    <div class="mb-[20px]">
      <el-button @click="handleAdd(null)" type="primary">
        <el-icon class="mr-[5px]"><Plus /></el-icon>
        新增
      </el-button>
      <!-- <el-button>
        <el-icon class="mr-[5px]"><Plus /></el-icon>
        新增管理费提取方案
      </el-button> -->
      <!-- <el-button> <svg-icon iconClass="import" class="mr-[5px]"></svg-icon>导入 </el-button> -->
    </div>
    <el-table :data="tableData" class="w-full" stripe v-loading="loading">
      <el-table-column prop="code" label="分类编号" width="120" />
      <el-table-column prop="belongType" label="项目分类" />
      <el-table-column label="项目级别" width="100" v-if="projectClass!='yfContract'">
        <template #default="{ row }">
          <CommonLabel :dataList="projectLevelList" :value="row.projectLevelId" />
        </template>
      </el-table-column>
      <el-table-column label="项目数" width="80">
        <template #default="{ row }">
          <el-link type="primary" @click="handleShowProject(row.id)">
            {{ row.totalProjectCount }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="预算标准">
        <template #default="{ row }">
          <div v-for="item in row.budgetStandardDateList" :key="item.id">
            <el-link type="primary" @click="handleShow(item.budgetStandardId)"
              >{{ item.budgetStandardName }}({{ formatDate(item.startDate, "YYYY") }} -
              {{ formatDate(item.endDate, "YYYY") }})</el-link
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="220" align="center">
        <template #default="{ row }">
          <el-button type="primary" size="small" plain @click="handleEdit(row)"> 编辑 </el-button>
          <el-button type="primary" size="small" plain @click="handleAdd(row)">
            添加子类
          </el-button>
          <!-- <el-button type="primary" size="small" plain @click="handleModeSet"
            >建卡模式配置</el-button
          > -->
          <!-- 不存在子节点并且项目数为0 才可以删除 -->
          <el-button
            type="danger"
            size="small"
            plain
            @click="handleDel(row.id)"
            v-if="!row.hasChildren && row.projectCount == 0"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-[20px] flex justify-end">
      <el-pagination
        v-bind="pagination"
        @update:current-page="currentChange"
        @update:page-size="sizeChange"
      />
    </div>
  </el-card>
  <EditModal ref="editModalRef" @save-success="search" />
  <ViewYsbzModal ref="viewYsbzModalRef" />
  <ProjectModal ref="projectModalRef" />
</template>

<script setup>
import useTable from "@/hooks/useTable";
import EditModal from "./components/EditModal.vue";
import ViewYsbzModal from "@/views/project/pages/ysbzsz/components/ViewModal.vue";
import ProjectModal from "./components/ProjectModal.vue";
import CommonLabel from "@/views/project/components/CommonLabel.vue";
import CommonSelect from "@/views/project/components/CommonSelect.vue";
import { getList, delRecords } from "@/api/project/zx/xmflsz";
import { formatDate } from "@/utils/formatTime";
import useCommonFetch from "@/hooks/useCommonFetch";
const editModalRef = ref();
const searchFormRef = ref();
const projectClass = inject("projectClass");
const viewYsbzModalRef = ref();
const projectModalRef = ref();
const searchForm = ref({
  code: "",
  name: "",
  projectLevelId: "",
});
const projectLevelList = useCommonFetch("项目级别");
const { loading, pagination, sizeChange, currentChange, tableData, handleDelete, search } =
  useTable((pagin) =>
    getList({
      pageNum: pagin.pageNum,
      pageSize: pagin.pageSize,
      ...searchForm.value,
      classId: projectClass,
    })
  );

//点击新增分类或添加子类
const handleAdd = (row) => {
  editModalRef.value.add(row);
};
//点击编辑
const handleEdit = (row) => {
  editModalRef.value.edit(row);
};
//点击删除
const handleDel = (id) => {
  handleDelete(delRecords, id);
};
//点击预算标准名称
const handleShow = (id) => {
  viewYsbzModalRef.value.view(id);
};
//点击项目数
const handleShowProject = (id) => {
  projectModalRef.value.view(id);
};
//点击重置
const resetForm = () => {
  searchFormRef.value.resetFields();
  search();
};
</script>

<style scoped lang="scss"></style>
